<?php
namespace Home\Controller;
class LawyerOrderController extends CommonController{

	public function _initialize(){
		parent::_initialize();
		$this->user_id();
		$this->add_step('订单中心', U('index'));
	}
	// 订单列表
	public function index(){
		$type = $this->_get('type');
		switch ($type) {
			case 'all':
				$this->add_step('全部订单');
				break;

			case 'wait':
				$where['status'] = 'wait-lawyer';
				$this->add_step('待处理订单');
				break;

			case 'doing':
				$where['status'] = array('in', 'doing-lawyer,doing-audit');
				$this->add_step('处理中订单');
				break;

			case 'finish':
				$where['status'] = 'finish';
				$this->add_step('已处理订单');
				break;
			
			default:
				
				break;
		}
		if($type != 'all'){
			$this->lawyer_is_role();
		}
		
		$this->assign('type', $type);
		// 订单号
		$order_sn = $this->_get('order_sn');
		if($order_sn){
			$where['order_sn'] = $order_sn;
		}
		$this->assign('order_sn', $order_sn);

		// 开始时间
		$start_date = $this->_get('start_date');
		if($start_date){
			$where['create_time'][] = array('egt',strtotime($start_date));
		}
		$this->assign('start_date', $start_date);

		// 结束时间
		$end_date = $this->_get('end_date');
		if($end_date){
			$where['create_time'][] = array('elt',strtotime($end_date . '23:59:59'));
		}
		$this->assign('end_date', $end_date);

		$where['lawyer_user_id'] = $this->user_id;
		$list = D('Order')->get_list($where, '', $this->_get('p', '', 1));
		//处理AJAX分页
		$this->page($list, D('Order')->count);

		$this->assign('lawyer_info', $this->user['lawyer_info']);
		$this->assign('doing_num', D('Order')->get_lawyer_doing_num($this->user_id));
		$this->assign('wait_num', D('Order')->get_lawyer_wait_num($this->user_id));
		$this->assign('list', $list);
		$this->display();
	}
	// 订单详情
	public function detail(){
		$this->add_step('订单详情');
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		// 点击处理改变订单状态
		D('Order')->lawyer_click_handle($order_sn, $this->user_id);
		// 订单详情
		$info = D('Order')->info($order_sn);
		if(!$info['order_sn'] || $info['lawyer_user_id'] != $this->user_id){
			$this->error('订单不存在！');
		}
		if(IS_POST){
			$finish_file_ids = implode(',', $this->_post('finish_file_ids'));
			if(substr($finish_file_ids, 0, 2) == '0,'){
				$finish_file_ids = substr($finish_file_ids, 2);
			}
			if(!$finish_file_ids){
				$this->error('请上传合同文件！');
			}
			if($info['status'] == 'doing-lawyer'){
				$data['finish_file_ids'] = $finish_file_ids;
				$data['status']          = 'doing-audit';//提交给平台审核
				$data['edit_time']       = time();
				$data['audit_status']    = 'wait';
				$data['audit_comment']   = '';

				$where['order_sn'] = $order_sn;
				D('Order')->where($where)->save($data);

				$this->success('提交成功，等待平台审核！', U('detail', array('order_sn' => $order_sn)));
			}
			$this->error('此订单状态不能保存！');
		}

		$this->assign('info', $info);
		$this->display();
	}
	// 添加订单TODO
	public function add_todo(){
		if(IS_POST){
			$content  = urldecode($this->_post('content', '请输入工作内容！'));
			$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
			$data = D('Order')->add_todo($order_sn, $this->user_id, $content);
			if($data){
				$this->success('添加成功！', '', $data);
			}else{
				$this->error(D('Order')->tips_info);
			}
		}
	}
	// 删除TODO
	public function del_todo(){
		$id = $this->_get('id', '对不起，您的操作有误！');
		$where['id']      = $id;
		$where['user_id'] = $this->user_id;
		D('OrderTodo')->where($where)->delete();
		$this->success('删除成功！');
	}
}